package com.cskaoyan.service;

import com.cskaoyan.bean.bo.*;
import com.cskaoyan.bean.vo.ConfigurationManagementVo;
import com.cskaoyan.mapper.MarketSystemMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.ArrayList;

/**
 * @Description:TODO
 * @author: landz
 * @create: 2022-12-30 11:48
 * @version:
 */
@Service
public class ConfigurationServiceImpl implements ConfigurationService {
    @Resource
    private MarketSystemMapper marketSystemMapper;


    /**
     * @param * @param null
     * @return {@link null}
     * @Description: 配置管理中的商场配置接口回显实现方法，回显数据查询逻辑
     * @version v1.0
     * @author landz
     * @date 2022/12/30 14:53
     */
    @Override
    public ConfigurationManagementVo mall() {
        ConfigurationManagementVo configurationManagementVo = new ConfigurationManagementVo();
        //分别查找需要的数据，再组合返回 此模块共用同一个sql语句即可
        configurationManagementVo.setMarket_mall_address(marketSystemMapper.selectSystemValueByKeyName("mall_address"));
        configurationManagementVo.setMarket_mall_longitude(marketSystemMapper.selectSystemValueByKeyName("mall_longitude"));
        configurationManagementVo.setMarket_mall_qq(marketSystemMapper.selectSystemValueByKeyName("mall_qq"));
        configurationManagementVo.setMarket_mall_name(marketSystemMapper.selectSystemValueByKeyName("mall_name"));
        configurationManagementVo.setMarket_mall_latitude(marketSystemMapper.selectSystemValueByKeyName("mall_latitude"));
        configurationManagementVo.setMarket_mall_phone(marketSystemMapper.selectSystemValueByKeyName("mall_phone"));
        return configurationManagementVo;
    }

    /**
     * @param * @param null
     * @return {@link null}
     * @Description: 配置管理中的运费配置接口回显实现方法，回显数据查询逻辑
     * @version v1.0
     * @author landz
     * @date 2022/12/30 16:42
     */
    @Override
    public ConfigurationManagementVo express() {
        //新建返回vo
        ConfigurationManagementVo configurationManagementVo = new ConfigurationManagementVo();
        //分别查找需要的返回数据 此模块共用同一个sql语句即可
        configurationManagementVo.setMarket_express_freight_min(marketSystemMapper.selectSystemValueByKeyName("express_freight_min"));
        configurationManagementVo.setMarket_express_freight_value(marketSystemMapper.selectSystemValueByKeyName("express_freight_value"));
        return configurationManagementVo;
    }

    /**
     * @param * @param null
     * @return {@link null}
     * @Description: 配置管理中的订单配置接口回显实现方法，回显数据查询逻辑
     * @version v1.0
     * @author landz
     * @date 2022/12/30 16:56
     */
    @Override
    public ConfigurationManagementVo order() {
        //新建返回vo
        ConfigurationManagementVo configurationManagementVo = new ConfigurationManagementVo();
        //分别查找需要的返回数据 此模块共用同一个sql语句即可
        configurationManagementVo.setMarket_order_comment(marketSystemMapper.selectSystemValueByKeyName("order_comment"));
        configurationManagementVo.setMarket_order_unconfirm(marketSystemMapper.selectSystemValueByKeyName("order_unconfirm"));
        configurationManagementVo.setMarket_order_unpaid(marketSystemMapper.selectSystemValueByKeyName("order_unpaid"));
        return configurationManagementVo;
    }

    /**
     * @param * @param null
     * @return {@link null}
     * @Description: 配置管理中的小程序配置接口回显实现方法，回显数据查询逻辑
     * @version v1.0
     * @author landz
     * @date 2022/12/30 17:10
     */
    @Override
    public ConfigurationManagementVo wx() {
        //新建返回的vo
        ConfigurationManagementVo configurationManagementVo = new ConfigurationManagementVo();
        //分别查找需要返回的数据  此模块共用同一个sql语句即可
        configurationManagementVo.setMarket_wx_catlog_goods(marketSystemMapper.selectSystemValueByKeyName("wx_catlog_goods"));
        configurationManagementVo.setMarket_wx_catlog_list(marketSystemMapper.selectSystemValueByKeyName("wx_catlog_list"));
        configurationManagementVo.setMarket_wx_index_brand(marketSystemMapper.selectSystemValueByKeyName("wx_index_brand"));
        configurationManagementVo.setMarket_wx_index_hot(marketSystemMapper.selectSystemValueByKeyName("wx_index_hot"));
        configurationManagementVo.setMarket_wx_index_new(marketSystemMapper.selectSystemValueByKeyName("wx_index_new"));
//        configurationManagementVo.setMarket_wx_share(marketSystemMapper.selectSystemValueByKeyName("wx_share"));
        boolean share = "true".equals(marketSystemMapper.selectSystemValueByKeyName("wx_share")) ? true : false;
        configurationManagementVo.setMarket_wx_share(share);
        configurationManagementVo.setMarket_wx_index_topic(marketSystemMapper.selectSystemValueByKeyName("wx_index_topic"));
        return configurationManagementVo;
    }
    /**
     * @param * @param null
     * @return {@link null}
     * @Description: 配置管理中的运费配置接口修改数据方法，数据修改逻辑
     * @version v1.0
     * @author landz
     * @date 2022/12/30 19:59
     */
    @Override
    public void updateExpress(ConfigExpressBo configExpressBo) {
        //通过反射获取类中的所有字段
        Field[] fields = configExpressBo.getClass().getDeclaredFields();
        ArrayList<ConfigurationBo> configurationBos = new ArrayList<>();
        for (Field field : fields) {
            if ("market_express_freight_min".equals(field.getName())) {
                ConfigurationBo configurationBo = new ConfigurationBo();
                configurationBo.setName(field.getName());
                configurationBo.setValue(configExpressBo.getMarket_express_freight_min());
                configurationBos.add(configurationBo);
            }
            if ("market_express_freight_value".equals(field.getName())) {
                ConfigurationBo configurationBo = new ConfigurationBo();
                configurationBo.setName(field.getName());
                configurationBo.setValue(configExpressBo.getMarket_express_freight_value());
                configurationBos.add(configurationBo);
            }
        }
        marketSystemMapper.updateByConfigExpressBoList(configurationBos);
    }

    /**
     * @param * @param null
     * @return {@link null}
     * @Description: 配置管理中的商场配置接口修改数据方法，数据修改逻辑
     * @version v1.0
     * @author landz
     * @date 2022/12/30 19:59
     */
    @Override
    public void updateMall(ConfigMallBo configMallBo) {
        //通过反射获取类中的所有字段
        Field[] fields = configMallBo.getClass().getDeclaredFields();
        //新建一个arraylist去接收新的configmallbos
        ArrayList<ConfigurationBo> configMallBos = new ArrayList<>();
        for (Field field : fields) {
            if ("market_mall_address".equals(field.getName())) {
                ConfigurationBo configurationBo = new ConfigurationBo();
                configurationBo.setName(field.getName());
                configurationBo.setValue(configMallBo.getMarket_mall_address());
                configMallBos.add(configurationBo);
            }
            if ("market_mall_latitude".equals(field.getName())) {
                ConfigurationBo configurationBo = new ConfigurationBo();
                configurationBo.setName(field.getName());
                configurationBo.setValue(configMallBo.getMarket_mall_latitude());
                configMallBos.add(configurationBo);
            }
            if ("market_mall_longitude".equals(field.getName())) {
                ConfigurationBo configurationBo = new ConfigurationBo();
                configurationBo.setName(field.getName());
                configurationBo.setValue(configMallBo.getMarket_mall_longitude());
                configMallBos.add(configurationBo);
            }
            if ("market_mall_name".equals(field.getName())) {
                ConfigurationBo configurationBo = new ConfigurationBo();
                configurationBo.setName(field.getName());
                configurationBo.setValue(configMallBo.getMarket_mall_name());
                configMallBos.add(configurationBo);
            }
            if ("market_mall_phone".equals(field.getName())) {
                ConfigurationBo configurationBo = new ConfigurationBo();
                configurationBo.setName(field.getName());
                configurationBo.setValue(configMallBo.getMarket_mall_phone());
                configMallBos.add(configurationBo);
            }
            if ("market_mall_qq".equals(field.getName())) {
                ConfigurationBo configurationBo = new ConfigurationBo();
                configurationBo.setName(field.getName());
                configurationBo.setValue(configMallBo.getMarket_mall_qq());
                configMallBos.add(configurationBo);
            }
        }
        marketSystemMapper.updateByConfigMallBoList(configMallBos);

    }

    /**
     * @Description: 配置管理中的订单配置接口修改数据方法，数据修改逻辑
     * @param  * @param null
     * @return {@link null}
     * @version v1.0
     * @author landz
     * @date 2022/12/30 22:59
     */
    @Override
    public void updateOrder(ConfigOrderBo configOrderBo) {
        //通过反射来获取类中的所有字段
        Field[] fields = configOrderBo.getClass().getDeclaredFields();
        //新建一个arraylist来接收这个新的bo
        ArrayList<ConfigurationBo> configOrderBos = new ArrayList<>();
        //for循环开启遍历，分别替换修改的内容
        for (Field field : fields) {
            if ("market_order_comment".equals(field.getName())) {
                ConfigurationBo configurationBo = new ConfigurationBo();
                configurationBo.setName(field.getName());
                configurationBo.setValue(configOrderBo.getMarket_order_comment());
                configOrderBos.add(configurationBo);
            }
            if ("market_order_unconfirm".equals(field.getName())) {
                ConfigurationBo configurationBo = new ConfigurationBo();
                configurationBo.setName(field.getName());
                configurationBo.setValue(configOrderBo.getMarket_order_unconfirm());
                configOrderBos.add(configurationBo);
            }
            if ("market_order_unpaid".equals(field.getName())) {
                ConfigurationBo configurationBo = new ConfigurationBo();
                configurationBo.setName(field.getName());
                configurationBo.setValue(configOrderBo.getMarket_order_unpaid());
                configOrderBos.add(configurationBo);
            }

        }
        marketSystemMapper.updateByConfigOrderBoList(configOrderBos);
    }

    /**
     * @Description: 配置管理中的小程序配置接口修改数据方法，数据修改逻辑
     * @param  * @param null
     * @return {@link null}
     * @version v1.0
     * @author landz
     * @date 2022/12/30 23:33
     */
    @Override
    public void updateWx(ConfigWxBo configWxBo) {
        //通过放射来获取类中的所有字段
        Field[] fields = configWxBo.getClass().getDeclaredFields();
        //新建一个arraylist来接收新的vo
        ArrayList<ConfigurationBo> configWxBos = new ArrayList<>();
        //for循环开启遍历，分别替换修改的内容
        for (Field field : fields) {
            if ("market_wx_index_new".equals(field.getName())) {
                ConfigurationBo configurationBo = new ConfigurationBo();
                configurationBo.setName(field.getName());
                configurationBo.setValue(configWxBo.getMarket_wx_index_new());
                configWxBos.add(configurationBo);
            }
            if ("market_wx_catlog_goods".equals(field.getName())) {
                ConfigurationBo configurationBo = new ConfigurationBo();
                configurationBo.setName(field.getName());
                configurationBo.setValue(configWxBo.getMarket_wx_catlog_goods());
                configWxBos.add(configurationBo);
            }
            if ("market_wx_catlog_list".equals(field.getName())) {
                ConfigurationBo configurationBo = new ConfigurationBo();
                configurationBo.setName(field.getName());
                configurationBo.setValue(configWxBo.getMarket_wx_catlog_list());
                configWxBos.add(configurationBo);
            }
            if ("market_wx_index_brand".equals(field.getName())) {
                ConfigurationBo configurationBo = new ConfigurationBo();
                configurationBo.setName(field.getName());
                configurationBo.setValue(configWxBo.getMarket_wx_index_brand());
                configWxBos.add(configurationBo);
            }
            if ("market_wx_index_hot".equals(field.getName())) {
                ConfigurationBo configurationBo = new ConfigurationBo();
                configurationBo.setName(field.getName());
                configurationBo.setValue(configWxBo.getMarket_wx_index_hot());
                configWxBos.add(configurationBo);
            }
            if ("market_wx_index_topic".equals(field.getName())) {
                ConfigurationBo configurationBo = new ConfigurationBo();
                configurationBo.setName(field.getName());
                configurationBo.setValue(configWxBo.getMarket_wx_index_topic());
                configWxBos.add(configurationBo);
            }
            if ("market_wx_share".equals(field.getName())) {
                ConfigurationBo configurationBo = new ConfigurationBo();
                configurationBo.setName(field.getName());
                configurationBo.setValue(String.valueOf(configWxBo.getMarket_wx_share()));
                configWxBos.add(configurationBo);
            }
        }
        marketSystemMapper.updateByConfigWxBoList(configWxBos);
    }


}
